package com.hospital.job.config;

import com.hospital.job.config.data.XxlJobProperty;
import com.xxl.job.core.executor.XxlJobExecutor;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@AutoConfiguration
@ConditionalOnBean(XxlJobExecutor.class)
@ConditionalOnProperty(prefix = "xxl.job",name = "enable",havingValue = "true",matchIfMissing = true)
@EnableConfigurationProperties
@EnableScheduling
@Slf4j
public class XxlJobConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public XxlJobExecutor xxlJobExecutor(XxlJobProperty xxlJobProperty){
        log.info("==[xxlJobExecutor][初始化执行器配置]");
        XxlJobProperty.AdminProperty admin=xxlJobProperty.getAdmin();
        XxlJobProperty.ExecutorProperty executorProperty=xxlJobProperty.getExecutor();

        XxlJobExecutor xxlJobExecutor=new XxlJobSpringExecutor();
        xxlJobExecutor.setIp(executorProperty.getIp());
        xxlJobExecutor.setPort(executorProperty.getPort());
        xxlJobExecutor.setAccessToken(executorProperty.getAppName());
        xxlJobExecutor.setLogPath(executorProperty.getLogPath());
        xxlJobExecutor.setLogRetentionDays(executorProperty.getLogRetentionDays());
        xxlJobExecutor.setAdminAddresses(admin.getAddresses());
        xxlJobExecutor.setAccessToken(xxlJobProperty.getAccessToken());
        return xxlJobExecutor;
    }
}
